﻿using System;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace NoteHelper
{
    public static class DockHelper
    {
        public static IDockContent FindDocument(this DockPanel dockPanel, string text)
        {
            var f = dockPanel.FindForm();
            if (f == null) return null;

            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                foreach (Form form in f.MdiChildren)
                    if (form.Text == text)
                        return form as IDockContent;

                return null;
            }
            else
            {
                foreach (IDockContent content in dockPanel.Documents)
                    if (content.DockHandler.TabText == text)
                        return content;

                return null;
            }
        }

        public static void ShowPanel(this DockContentHandler dockHandler, string className)
        {
            Type formType = typeof(MainForm).Assembly.GetType(className);
            if (formType == null)
            {
                AlterMsg.Info(string.Format("{0}不存在！", className));
                return;
            }
            var form = (DockContent)Activator.CreateInstance(formType);
            if (form == null) return;

            var content = dockHandler.DockPanel.FindDocument(form.Text);
            if (content != null)
            {
                content.DockHandler.Activate();
            }
            else
            {
                form.Show(dockHandler.DockPanel);
            }
        }
    }
}
